package cn.edu.scau.cmil.luochenghao.comprehensive;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Record {
	public static void record(String[][] student) {
		JFrame x = new JFrame("确认信息");//先输入课程、教师、专辑班级和人数
		x.setLayout(new FlowLayout());
		JButton buttonSure = new JButton("确定");
		JTextField t1 = new JTextField(30);
		JTextField t2 = new JTextField(17);
		JTextField t3 = new JTextField(30);
		JTextField t4 = new JTextField(15);
		buttonSure.setFont(new Font("黑体", Font.PLAIN, 12));
		buttonSure.setVisible(true);
		x.add(new JLabel("请输入课程名称："));
		x.add(t1);
		x.add(new JLabel("请输入教师姓名："));
		x.add(t3);
		x.add(new JLabel("请输入专业班级(如：20XX级XXX专业XX班)："));
		x.add(t2);
		x.add(new JLabel("请输入学生人数(一次最多输入10名学生的数据)："));
		x.add(t4);
		x.add(buttonSure);
		x.setResizable(false);
		x.setSize(525, 190);
		x.setLocationRelativeTo(null);
		x.setVisible(true);
		x.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		buttonSure.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int number = Integer.parseInt(t4.getText().trim());
				
				if (number <= 10) {//一次只能记录10条成绩
					int renshu=0;
			  	  	for(int k=0;student[k][0]!=null;k++) {
			  	  		renshu++;
				  	}
					x.setVisible(false);
					JFrame f = new JFrame("录入信息");//再输入四项成绩
					JPanel fr = new JPanel();
					fr.setLayout(null);//手动设置每个组件位置
					int i;
					for (i = 0; i < number; i++) {
						JLabel label1 = new JLabel("学生" + (i + 1) + "：");
						label1.setBounds(10, 5 + i * 45, 80, 20);
						fr.add(label1);
						JLabel label2 = new JLabel("学号：");
						label2.setBounds(10, 25 + i * 45, 50, 20);
						fr.add(label2);
						JTextField text1 = new JTextField();
						text1.setBounds(50, 25 + i * 45, 120, 20);
						fr.add(text1);

						JLabel label3 = new JLabel("姓名：");
						label3.setBounds(180, 25 + i * 45, 50, 20);
						fr.add(label3);
						JTextField text2 = new JTextField();
						text2.setBounds(210, 25 + i * 45, 60, 20);
						fr.add(text2);

						JLabel label4 = new JLabel("考勤：");
						label4.setBounds(280, 25 + i * 45, 50, 20);
						fr.add(label4);
						JTextField text3 = new JTextField();
						text3.setBounds(310, 25 + i * 45, 40, 20);
						fr.add(text3);

						JLabel label5 = new JLabel("测验：");
						label5.setBounds(360, 25 + i * 45, 50, 20);
						fr.add(label5);
						JTextField text4 = new JTextField();
						text4.setBounds(390, 25 + i * 45, 40, 20);
						fr.add(text4);

						JLabel label6 = new JLabel("作业：");
						label6.setBounds(440, 25 + i * 45, 50, 20);
						fr.add(label6);
						JTextField text5 = new JTextField();
						text5.setBounds(470, 25 + i * 45, 40, 20);
						fr.add(text5);

						JLabel label7 = new JLabel("期末：");
						label7.setBounds(520, 25 + i * 45, 50, 20);
						fr.add(label7);
						JTextField text6 = new JTextField();
						text6.setBounds(550, 25 + i * 45, 40, 20);
						fr.add(text6);
						//输入完期末成绩（text6失去焦点）后，把text1-5记录到数组对应位置，
						text6.addFocusListener(new FocusListener(){
							public void focusLost(FocusEvent e) {
								int renshu=0;
						  	  	for(int k=0;student[k][0]!=null;k++) {
						  	  		renshu++;
							  	}
						  	  student[renshu][0]=text1.getText().trim();
								student[renshu][1]=text2.getText().trim();
								student[renshu][2]=text3.getText().trim();
								student[renshu][3]=text4.getText().trim();
								student[renshu][4]=text5.getText().trim();
								student[renshu][5]=text6.getText().trim();
								//总评等于四项成绩总和
								student[renshu][6]=(String.valueOf(Integer.parseInt(student[renshu][2])+
										Integer.parseInt(student[renshu][3])+Integer.parseInt(student[renshu][4])+Integer.parseInt(student[renshu][5])));
								//课程、教师、专业班级
								student[renshu][7]=t1.getText().trim();
								student[renshu][8]=t3.getText().trim();
								student[renshu][9]=t2.getText().trim();
								//输入完期末就不可更改
								text1.setEditable(false);
								text2.setEditable(false);
								text3.setEditable(false);
								text4.setEditable(false);
								text5.setEditable(false);
								text6.setEditable(false);
							}
							public void focusGained(FocusEvent e) {
				            }
						});
					}
					JLabel label=new JLabel("注：期末成绩输入完毕后前面所有项将无法编辑");
					label.setBounds(10, 25+i*45, 400, 20);
					label.setForeground(Color.RED);
					fr.add(label);
					JButton buttonSure1 = new JButton("确定");
					buttonSure1.setBounds(520, 25+i*45, 60, 20);
					buttonSure1.setFont(new Font("黑体", Font.PLAIN, 12));
					buttonSure1.setVisible(true);
					fr.add(buttonSure1);
					f.setSize(600, number * 45 + 85);//根据成绩条数设置窗口大小
					f.setLocationRelativeTo(null);
					f.setContentPane(fr);
					f.setResizable(false);
					f.setLocationRelativeTo(null);
					f.setVisible(true);
					f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
					buttonSure1.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							f.setVisible(false);
						}
					});
				}
			}
		});
	}
}
